using System.Collections.Generic;
using UnityEngine;

namespace ai_library
{
	public class GlobalMessagingState<T>: IState<T> where T : IEntity, IEntityStateMachine<T>
	{
		private static readonly GlobalMessagingState<T> instance = new GlobalMessagingState<T>();

		private List<IOpportunity<T>> m_opportunities = new List<IOpportunity<T>>();

		private GlobalMessagingState()
		{
			try
			{
				m_opportunities.Add( 
					OpportunityBuilder<T>.Build(	ConditionTypes.RandomRange_0_10, 
				                          			ActionTypes.SendMsgRand)
	            );
			}
			catch(OpportunityBuilderException e)
			{
				throw new StateException(e.ToString());
			}

		}

		public static GlobalMessagingState<T> Instance
		{
			get
			{
				return instance;
			}
		}
		
		public void Enter(T t)
		{
			Debug.Log ("GlobalMessagingState: Enter " + t.ID);
		}
		
		public void Update(T t, float dt)
		{
			Debug.Log ("GlobalMessagingState: Update " + t.ID);

			try
			{
				foreach (IOpportunity<T> o in m_opportunities)
					o.IsSatisfied(t);
			}
			catch(OpportunityException e)
			{
				throw new StateException(e.ToString());
			}
		}
		
		public void Exit(T t)
		{
			Debug.Log ("GlobalMessagingState: Exit " + t.ID);
		}
		
		public bool OnMessage(T t, Message m)
		{
			Debug.Log("GlobalMessagingState: OnMessage:  Agent: " + t.ID + " Received message from: " + m.SenderId + " Message: " + m.Msg);

			return false;
		}
	}
}

